﻿using System.Linq;

namespace Repo.Converters
{
    public static class DirectPropertyMapper
    {
        public static TMapTo Map<TMapTo, TMapFrom>(TMapFrom mapFrom) where TMapTo : new()
        {
            var newT = new TMapTo();
            foreach (var propertyMapTo in typeof(TMapTo).GetProperties())
            {
                foreach (var value in from propertyMapFrom in typeof (TMapFrom).GetProperties()
                                      where propertyMapFrom.Name == propertyMapTo.Name
                                      select propertyMapFrom.GetValue(mapFrom, null))
                {
                    if (propertyMapTo.CanWrite)
                    {
                        propertyMapTo.SetValue(newT, value, null);
                    }
                }
            }

            return newT;
        }
    }
}
